package com.fjd.service.impl;

import com.fjd.entity.*;
import com.fjd.mapper.*;
import com.fjd.service.ProductService;
import com.fjd.vo.Category4VO;
import com.fjd.vo.MerchandiseInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
public class ProductServiceImpl implements ProductService {
    @Autowired
    private MerchandiseInfoMapper merchandiseInfoMapper;
    @Autowired
    private ProductImageMapper productImageMapper;
    @Autowired
    private ShoppingProductMapper shoppingProductMapper;
    @Autowired
    private DiscountDetailMapper discountDetailMapper;
    @Autowired
    private DiscountMapper discountMapper;
    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    /**
     * 根据商品id获取商品信息
     * @param productId
     * @return
     */
    public MerchandiseInfoVO getProductById(String productId) {


        //TODO 这个接口传进来的参数实际是第三级商品的ID，应该为merchandiseId更加合理

        log.info("根据商品id获取商品信息");

        MerchandiseInfoVO merchandiseInfoVO = new MerchandiseInfoVO();

        //获取merchandise相关数据
        MerchandiseInfo merchandiseInfo = merchandiseInfoMapper.getById(productId);
        BeanUtils.copyProperties(merchandiseInfo,merchandiseInfoVO);

        //获取merchandise对应的图片
        merchandiseInfoVO.setMerchandiseImageList(productImageMapper.getByProductId(productId));

        //设置目录为中文名字 而不是目录编号
        String categoryId = merchandiseInfoVO.getMerchandiseCategory();
        merchandiseInfoVO.setMerchandiseCategory(productCategoryMapper.getCategoryNameByCategoryId(categoryId));

        List<Goods> goodsList = shoppingProductMapper.getByParentId(productId);
        Discount discount = discountMapper.getByMerchandiseIdAndProcessing(productId);
        System.out.println("discount");
        System.out.println(discount);
        if(discount != null) {
            for (Goods good : goodsList) {
                DiscountDetail discountDetail = discountDetailMapper.getDiscountPriceByProductIdAndDiscountId(good.getProductId(), discount.getId());
                if(discountDetail != null) {
                    good.setDiscountPrice(discountDetail.getDiscountPrice());
                }
            }
        }
        //获取merchandise下所有规格的product
        merchandiseInfoVO.setGoods(goodsList);

        return merchandiseInfoVO;
    }

    @Override
    public List<Category4VO> getProductMenu() {
        return productCategoryMapper.getInfo3();
    }

    @Override
    public List<Category4VO> getProductSpec(String productId) {
        return productCategoryMapper.getInfoByParentId(productId);
    }

    @Override
    public void addProduct(MerchandiseInfo merchandiseInfo) {
        String category2 = productCategoryMapper.getParentNameById(merchandiseInfo.getMerchandiseId());
        String category1 = productCategoryMapper.getParentNameById(category2);
        merchandiseInfo.setMerchandiseCategory(category1);
        merchandiseInfoMapper.addMerchandise(merchandiseInfo);
    }

    @Override
    public void updatePrice(String specId, Integer price) {
        productCategoryMapper.updatePrice(specId, price);
    }

    @Override
    public List<MerchandiseInfo> getAllMerchandiseInfo() {
        return merchandiseInfoMapper.getAllMerchandise();
    }

    @Override
    public void editProduct(MerchandiseInfo merchandiseInfo) {
        merchandiseInfoMapper.editMerchandise(merchandiseInfo);
    }

    @Override
    public boolean uploadImage(String procId, String imagePath) {
        return merchandiseInfoMapper.addMerchandiseImage(procId, imagePath, 1);
    }
}
